package Handler;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DAO.CombustibleDao;
import Modelo.Combustible;


@WebServlet("/CombustibleHandler")
public class CombustibleHandler extends HttpServlet {
	private static final long serialVersionUID = 1L;
     
	private static String ADD = "/CombustibleAdd.jsp";
    private static String Edit = "/Combustibleedit.jsp";
    private static String CombustiblesRecord="/ListCombustible.jsp";
    private CombustibleDao dao;
    
    public CombustibleHandler() {
        super();
        dao = new CombustibleDao();
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String redirect="";
        String combId = request.getParameter("Combustibleid");        
        String action = request.getParameter("action");
        
		if (action.equalsIgnoreCase("editform")){         
		        redirect = Edit;            
		}
		else if (action.equalsIgnoreCase("addnew")){         
		        redirect = ADD;            
		}else if (action.equalsIgnoreCase("listCombustible")) {
			redirect = CombustiblesRecord;
			request.setAttribute("Combustible", dao.getAllCombustibles());
		} 
		
		 RequestDispatcher rd = request.getRequestDispatcher(redirect);
		 rd.forward(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String redirect="";
        String combId = request.getParameter("Combustibleid");        
        String action = request.getParameter("action");
        
		if (action.equalsIgnoreCase("insert")) {
			Combustible objCombustible = new Combustible();
			objCombustible.setNombre_Combustible(request.getParameter("Nombre_Combustible"));
			dao.addCombustible(objCombustible);
			redirect = CombustiblesRecord;
			request.setAttribute("Combustible", dao.getAllCombustibles());
			System.out.println("Record Added Successfully");
		}

		else if (action.equalsIgnoreCase("delete")) {
			String comb_Id = request.getParameter("CombustibleId");
			int combid = Integer.parseInt(comb_Id);
			dao.removeCombustible(combid);
			redirect = CombustiblesRecord;
			request.setAttribute("Combustible", dao.getAllCombustibles());
			System.out.println("Record Deleted Successfully");

		} else if (action.equalsIgnoreCase("edit")) {
			String comb_Id = request.getParameter("CombustibleId");
			int combid = Integer.parseInt(comb_Id);
			Combustible objCombustible = new Combustible();
			objCombustible.setCombustibleId(combid);
			objCombustible.setNombre_Combustible(request.getParameter("Nombre_Combustible"));
			dao.editCombustible(objCombustible);
			request.setAttribute("Combustible", objCombustible);
			redirect = CombustiblesRecord;
			System.out.println("Record updated Successfully");

		} else if (action.equalsIgnoreCase("listCombustibles")) {
			redirect = CombustiblesRecord;
			request.setAttribute("Combustibles", dao.getAllCombustibles());
		} else {
			redirect = ADD;
		}

    RequestDispatcher rd = request.getRequestDispatcher(redirect);
    rd.forward(request, response);
	}

}
